//
//  DatabaseSql.m
//  微他
//
//  Created by 微他 on 14-8-5.
//  Copyright (c) 2014年 微他. All rights reserved.
//

#import "DatabaseSql.h"

@implementation DatabaseSql

//执行sql
+ (void)excute:(NSString *)sqlString
{
    DbHelper *dbhelper = [DbHelper instance];
    if (sqlString != nil) {
        [dbhelper executeByQueue:sqlString];
    }
}
//执行sql查询
+ (NSArray *)query:(NSString *)sqlString
{
    DbHelper *dbhelper = [DbHelper instance];
    if (sqlString == nil) {
        return nil;
    }
    return [dbhelper queryDbToDictionaryArray:nil sql:sqlString];
}

//执行reginsql
+ (void)regionexcute:(NSString *)sqlString
{
    DbHelper *dbhelper = [DbHelper instanceregion];
    if (sqlString != nil) {
        [dbhelper executeByQueue:sqlString];
    }
}
//执行reginsql查询
+ (NSArray *)regionquery:(NSString *)sqlString
{
    DbHelper *dbhelper = [DbHelper instanceregion];
    if (sqlString == nil) {
        return nil;
    }
    return [dbhelper queryDbToDictionaryArray:nil sql:sqlString];
}

//转义'
+ (NSString *)esc:(NSString *)string
{
    NSMutableString *escString = [NSMutableString stringWithFormat:@"%@",string];
    for (int i=0; i<escString.length; i++) {
//        NSString *ch=[escString characterAtIndex:i];
        NSString *ch = [escString substringWithRange:NSMakeRange(i, 1)];
        if ([ch isEqualToString: @"\'"]) {
            [escString replaceCharactersInRange:NSMakeRange(i, 1) withString:@"''"];
            i++;
        }
    }
    return escString;
}

#pragma user.sqlite 查询



#pragma region.sqlite 查询

+ (NSArray *)region_queryregionofcity:(NSString *)curcityid{
    NSArray *regionsarray=[[NSArray alloc] init];
    NSString *regionsql=[NSString stringWithFormat:@"select toid from link where fromid=%@",curcityid];
    NSArray *fromids=[DatabaseSql regionquery:regionsql];
    if (fromids.count>0) {
        NSMutableArray *curarray=[[NSMutableArray alloc] init];
        for (NSDictionary *cur in fromids) {
            [curarray addObject:cur[@"toid"]];
        }
        NSString *regionnamesql=[NSString stringWithFormat:@"select fullname,id,level from def where id in(%@)",[curarray componentsJoinedByString:@","]];
        regionsarray=[DatabaseSql regionquery:regionnamesql];
    }
    return regionsarray;
}

+ (NSArray *)region_queryallregion{
    NSString *sql=[NSString stringWithFormat:@"select name,fullname,id as dataid from def where level<=2"];
    return [DatabaseSql regionquery:sql];
}

+ (NSMutableDictionary *)region_queryidusename:(NSString *)regionname{
    NSString *sql=[NSString stringWithFormat:@"select id as dataid,name from def where fullname='%@'",regionname];
    NSArray *curarray=[DatabaseSql regionquery:sql];
    if (curarray.count>0) {
        NSMutableDictionary *curdic=curarray[0];
        [curdic setObject:regionname forKey:@"fullname"];
        return curdic;
    }
    return nil;
}

+ (NSString *)region_queryfromidusetoid:(NSString *)toid{
   NSString *sql=[NSString stringWithFormat:@"select fromid from link where toid='%@'",toid];
    NSArray *curarray=[DatabaseSql regionquery:sql];
    if (curarray.count>0) {
        return curarray[0][@"fromid"];
    }
    return nil;
}

//tqh add

+ (NSArray *)queryAllProvinces {
    NSString *sql=[NSString stringWithFormat:@"select proname,shortname,id from user_province "];
    return [DatabaseSql query:sql];
}

+ (NSArray *)queryCityForProvinces:(NSString *)provinceId {
    NSString *regionsql=[NSString stringWithFormat:@"select cityname as name,id from user_city where provid='%d'",[provinceId intValue]];
    NSArray *fromids=[DatabaseSql query:regionsql];

    return fromids;
}

+ (NSArray *)queryDistrict:(NSString *)Id {
    NSString *sql=[NSString stringWithFormat:@"select distname from user_district  where provid='%d'",[Id intValue]];
    return [DatabaseSql query:sql];
}
+ (NSArray *)findprovid:(NSString *)provid
                 cityID:(NSString *)cityId {
    NSString *sqlString = [NSString stringWithFormat:@"select  id,distname as name, cityid,provid from user_district where provid = %@ and cityid = %@",provid,cityId];
    return [DatabaseSql query:sqlString];
}
@end
